package com.kxiang.photopselect;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.kxiang.imageselector.PhotoPickerActivity;
import com.kxiang.imageselector.utils.PhotoPickerIntent;

import java.util.ArrayList;
import java.util.List;

public class PhotoSelectAtivity extends FragmentActivity {
    private RecyclerView rlv_dishes_image;
    public final static int REQUEST_CODE = 1;
    private DishesImageAdaper imageAdaper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_select);
        rlv_dishes_image = (RecyclerView) findViewById(R.id.rlv_dishes_image);


        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);

        rlv_dishes_image.setLayoutManager(gridLayoutManager);

        imageAdaper = new DishesImageAdaper(this);
        rlv_dishes_image.setAdapter(imageAdaper);


        List<String> list = new ArrayList<>();
//        for (int i = 0; i < 6; i++) {
//            list.add("数据" + i);
//        }
        imageAdaper.SetData(list);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("photos", "requestCode:" + requestCode);
        List<String> photos = null;
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
            if (data != null) {
                photos = data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS);
            }
            selectedPhotos.clear();

            if (photos != null) {

                selectedPhotos.addAll(photos);
                imageAdaper.SetData(selectedPhotos);
                Log.e("photos", "photos:" + selectedPhotos.get(0));
            }
        }
    }


    ArrayList<String> selectedPhotos = new ArrayList<>();

    protected class DishesImageAdaper extends RecyclerView.Adapter {

        private List<String> imagePath;
        private LayoutInflater inflater;
        private Context context;

        public DishesImageAdaper(Context context) {
            this.context = context;
            inflater = LayoutInflater.from(context);
        }

        public void SetData(List<String> imagePath) {
            this.imagePath = imagePath;
            notifyDataSetChanged();
        }

        public class ImageShowHoler extends RecyclerView.ViewHolder {
            public ImageView iv_dishes_one;

            public ImageShowHoler(View itemView) {
                super(itemView);
                iv_dishes_one = (ImageView) itemView.findViewById(R.id.iv_dishes_one);
            }
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            return new ImageShowHoler(inflater.inflate(R.layout.item_dishes_image, parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ImageShowHoler imageShowHoler = (ImageShowHoler) holder;

            if (imagePath.size() == 6) {
                Glide.with(context)
                        .load(imagePath.get(position))
                        .into(imageShowHoler.iv_dishes_one);
            } else {
                if (position == imagePath.size()) {
                    imageShowHoler.iv_dishes_one.setImageResource(R.drawable.add);
                } else {
                    Glide.with(context)
                            .load(imagePath.get(position))
                            .into(imageShowHoler.iv_dishes_one);
                }

            }
            imageShowHoler.iv_dishes_one.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(PhotoSelectAtivity.this, PhotoPickerActivity.class);
                    PhotoPickerIntent.setPhotoCount(intent, 6);
                    PhotoPickerIntent.setShowCamera(intent, true);
                    PhotoPickerIntent.setSelected(intent, selectedPhotos);
                    startActivityForResult(intent, REQUEST_CODE);
                }
            });

        }

        @Override
        public int getItemCount() {
            if (imagePath == null) {
                return 0;
            } else if (imagePath.size() == 6) {
                return imagePath.size();
            } else {
                return imagePath.size() + 1;
            }

        }
    }
}
